package com.yjy.controller;
// Tomcat 10+ 写法

import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.github.pagehelper.PageInfo;
import com.yjy.common.Result;
import com.yjy.entity.Introduction;
import com.yjy.service.IntroductionService;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/introduction")
public class IntroductionController {

    @Resource
    IntroductionService introductionService;

    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Introduction introduction = introductionService.selectById(String.valueOf(id));
        return  Result.success(introduction);
    }

    @GetMapping("/selectAll")
    public Result selectAll(){
        List<Introduction> introductions = introductionService.selectAll(null);
        return Result.success(introductions);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Introduction introduction) {//@RequestBody 接受前端传来的json参数 get是接受不到json的 post delete put可以接受
        introductionService.add(introduction);
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(@RequestBody Introduction introduction) {//@RequestBody 接受前端传来的json参数 get是接受不到json的 post delete put可以接受
        introductionService.update(introduction);
        return Result.success();
    }

    @DeleteMapping ("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        introductionService.deleteById(id);
        return Result.success();
    }




    /**
     * 分页查询
     * pageNum: 当前的页码
     * pageSize: 每页的个数
     */
    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1")Integer pageNum,
                             @RequestParam(defaultValue = "10")Integer pageSize,
                             Introduction introduction)
    {
                             //@RequestParam(required = false)String name){//也可以Introduction introduction
        PageInfo<Introduction> introductionPageInfo = introductionService.selectPage(pageNum, pageSize,introduction);
        return Result.success(introductionPageInfo); //返回的是分页的对象
    }

}
